#ifndef Terrain_h__
#define Terrain_h__

#include <string>
#include <vector>
#include <d3dx9.h>
#include "../base/Vertex.h"

class Terrain
{
public:
	Terrain(
		IDirect3DDevice9*  device,
		std::string  heightmapFileName,
		int numVertsPerRow,
		int numVertsPerCol,
		int cellSpacing,
		float heightScale);
	~Terrain();

	int  GetHeightmapEntry(int row, int col);
	void SetHeightmapEntry(int row, int col, int value);

	float GetHeight(float x, float z);
	float GetHeightVerbose(float x, float z);

	bool LoadTexture(std::string fileName);
	bool GenerateTexture(D3DXVECTOR3* directionToLight);
	bool Draw(D3DXMATRIX *world, bool drawTris);

private:
	typedef Vertex_PT  TerrainVertex;

	IDirect3DDevice9*       m_device;
	IDirect3DTexture9*      m_texture;
	IDirect3DVertexBuffer9* m_vb;
	IDirect3DIndexBuffer9*  m_ib;

	int m_numVertsPerRow;
	int m_numVertsPerCol;
	int m_cellSpacing;


	int m_numCellsPerRow;
	int m_numCellsPerCol;
	int m_width;
	int m_depth;
	int m_numVertices;
	int m_numTriangles;

	float m_heightScale;

	std::vector<int>  m_heightmap;

	// helper methods
	bool  _ReadRawFile(std::string fileName);
	bool  _ComputeVertices();
	bool  _ComputeIndices();
	bool  _LightTerrain(D3DXVECTOR3* directionToLight);
	float _ComputeShade(int cellRow, int cellCol, D3DXVECTOR3* directionToLight);

};



#endif // Terrain_h__